using System;
using System.Collections.Generic;
using System.Text;
using SharksHunter.Domain.Model;
using SharksHunter.TransferObjects.Presentation;
using SharksHunter.Engine.Components;

namespace SharksHunter.Controller.Presenter
{
    public class CollectionPresenter: Interfaces.ICollectionPresenter
    {
        #region MEMEBRS
        private SharksHunter.Service.Interfaces.IHunterCollectionService    _hunterService;
        private Interfaces.ICollectionView                                  _hunterView;
        #endregion

        #region CTOR
        public CollectionPresenter(Interfaces.ICollectionView view)
        {
            _hunterView     = view;
            _hunterService = Factory.CollectionFactory.CreateHunterService();
        }
        #endregion

        #region ICollectionManager


        Collection Interfaces.ICollectionPresenter.OpenCollection()
		{
            try
            {
                SharksHunter.TransferObjects.Presentation.IParamProvider pprov = _hunterView.QueryOpenCollectionFile();
                return _hunterService.OpenCollection(pprov);
            }
             catch(Exception ex)
            {
                this._hunterView.Log(SharksHunter.TransferObjects.Presentation.LogPriority.Critical, ex.ToString());
            }
            return null;

		}

		Collection Interfaces.ICollectionPresenter.NewCollection()
		{
            try
            {
                SharksHunter.TransferObjects.Presentation.IParamProvider prov = _hunterView.QueryNewCollectionParam();
                Collection c = _hunterService.NewCollection(prov);
                this._hunterView.DataSource = this._hunterService.CollectionList;
                return c;
            }
            catch(Exception ex)
            {
                this._hunterView.Log(SharksHunter.TransferObjects.Presentation.LogPriority.Critical, ex.ToString());
                return null;
            }
            
		}
		void Interfaces.ICollectionPresenter.SaveCollection()
		{
            try
            {
                _hunterService.SaveCollection();
            }
             catch(Exception ex)
            {
                this._hunterView.Log(SharksHunter.TransferObjects.Presentation.LogPriority.Critical, ex.ToString());
            }

        }
        void Interfaces.ICollectionPresenter.AddEntity()
        {
            try
            {
                SharksHunter.TransferObjects.Presentation.IParamProvider pprov = _hunterView.QueryNewEntityFile();
                string[] files = pprov.QueryParam(ParamsNames.FileNames);
                foreach (string file in files)
                {
                    SharksHunter.Engine.CSV.CSVScanner scanner = new SharksHunter.Engine.CSV.CSVScanner(file);
                    _hunterService.NewEntity(SharksHunter.Engine.CSV.Tools.ExtractEntityFromCSVName(file), scanner);
                }
            }
             catch(Exception ex)
            {
                this._hunterView.Log(SharksHunter.TransferObjects.Presentation.LogPriority.Critical, ex.ToString());
            }
        }
        void Interfaces.ICollectionPresenter.RemoveEntity(string entname)
        {
        }
        void Interfaces.ICollectionPresenter.AssignToView(Interfaces.ICollectionView view)
        {
        }

        void Interfaces.ICollectionPresenter.Hunt()
        {
            SharksHunter.Engine.Interfaces.IHunterComponent pif;
            SharksHunter.Engine.Interfaces.IHunterComponent collector;

            pif = new PrepareIncomingFiles(this._hunterService.GetHuntInfo());

            collector = new Collector((SharksHunter.Service.Interfaces.IFetchTranferObject)this._hunterService);
            collector.SyncObjectExt = pif.SyncObject;

            pif.DoJob();
            collector.DoJob();
        }
        #endregion
    }
}
